//
//  CouponDetailVC.m
//  HengYouCai
//
//  Created by ycgwl on 2017/5/3.
//  Copyright © 2017年 lwj. All rights reserved.
//

#import "ZYRedBagVCViewController.h"
#import "RedPackModel.h"
#import "BannerWebViewVC.h"
#import "HengYouCai-Swift.h"

@interface ZYRedBagVCViewController ()<UITableViewDelegate,UITableViewDataSource>
{
    BOOL _isLoseEfficacy;
    NSInteger _pageNo;
}
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (nonatomic, retain)NSMutableArray *totalArr;
@property (weak, nonatomic) IBOutlet UILabel *tishiLab;

//@property(nonatomic,assign)BOOL redIsValid;
/**
 可使用 已过期
 */
@property (nonatomic, copy)NSString *status;

@end

@implementation ZYRedBagVCViewController

- (void)dealloc{
    NSLog(@"CouponDetailVC dealloc");
}
- (void)viewDidLoad {
    [super viewDidLoad];
        _isLoseEfficacy = YES;
//    self.redIsValid = true;
    _status = @"ksy";
    _pageNo = 1;
    self.view.backgroundColor = Normal_BackColor;
    [self.view showHUDLoading];
    [self netWorking];
    
    [self startRefresh];
    
    self.tableView.mj_footer.hidden = YES;
    [self creatNav];
    
}
- (void)creatNav{
    self.title = self.titleName;
    UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
    [button setTitle:@"规则说明" forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14];
    [button setTitleColor:Normal_333Color forState:UIControlStateNormal];
    [button addTarget:self action:@selector(rightButtonClick) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc]initWithCustomView:button];
    UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, f_Device_w, 10)];
    lineView.backgroundColor = Normal_BackColor;
    self.tableView.tableHeaderView = lineView;
}
- (void)rightButtonClick{
    BannerWebViewVC *vc = [[BannerWebViewVC alloc]initWithUrlStr:HYC_RedbagRuler title:self.titleName];
    [self pushVC:vc];
    
}
//- (void)updateViewConstraints
//{
//    [super updateViewConstraints];
//    _leftValue.constant = f_i6real(140);
//
//}
#pragma mark- 刷新
- (void)startRefresh
{
    __weak ZYRedBagVCViewController * weakSelf = self;
    MJRefreshNormalHeader *header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        ZYRedBagVCViewController * strongSelf = weakSelf;
        strongSelf ->_pageNo = 1;
        [strongSelf netWorking];
        
    }];
    header.backgroundColor = Normal_BackColor;
    self.tableView.backgroundColor = Normal_BackColor;
    self.tableView.mj_header = header;
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        ZYRedBagVCViewController * strongSelf = weakSelf;
        strongSelf -> _pageNo++;
        [strongSelf netWorking];
    }];
    
}

/**
 <#Description#>

 @param CGFloat <#CGFloat description#>
 @return <#return value description#>
 */
#pragma tableviewDatasource
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 135;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    //    if (_tishiLab) {
    //
    //        if (_isLoseEfficacy) {
    //            _tishiLab.text = [_type isEqualToString:@"0"]?@"已无更多失效红包":@"已无更多失效加息券";
    //        }else{
    //            _tishiLab.text = [_type isEqualToString:@"0"]?@"已无更多可用红包":@"已无更多可用加息券";
    //        }
    //    }
    
    return _totalArr.count;
}
#pragma tableviewDelegate

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZYReturnRedbagVCCell * cell = [ZYReturnRedbagVCCell wl_createCellWithTableView:tableView];
    [cell wl_configurationCell:self.totalArr :tableView :indexPath];
//    [cell wl_CouponconfigurationCellWithModelTemp:self.totalArr listView:tableView indexPath:indexPath];
    //    [cell wl_CouponConfigurationCellWithModelTemp:self.totalArr listView:tableView indexPath:indexPath];
    //    RedPackCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CouponDetailCellID"];
    //    cell.type = _type;
    //    cell.isLoseEfficacy = _isLoseEfficacy;
    //    cell.model = _totalArr[indexPath.row];
    return cell;
}

/**
 已失效 可使用
 */
- (IBAction)isEfficacy:(UIButton *)sender {
    // 防止多次点击
    [self.view showHUDLoading];
    _pageNo=1;
    _isLoseEfficacy = !_isLoseEfficacy;
    
    
    [sender setTitle:_isLoseEfficacy?@"查看已失效":@"查看可使用"forState:UIControlStateNormal];
    [self netWorking];
}
- (NSMutableArray *)totalArr
{
    if (!_totalArr) {
        _totalArr = [NSMutableArray array];
    }
    return _totalArr;
}
//- (void)setType:(NSString *)type
//{
//    _type = type;
//



//}
- (void)netWorking
{
    
    
    WLGetWeakSelf(weakSelf, self);
//    NSDictionary *dic = @{"type":"0","flag":self.redIsValid ?"ksy":"ysx","pageNo":self.pageManager.pageNo,"pageSize":self.pageManager.pageSize};
    NSDictionary *dic = @{@"type":@"0",@"flag":_isLoseEfficacy ?@"ksy":@"ysx",@"pageNo":[NSString stringWithFormat:@"%ld",_pageNo],@"pageSize":@"10"};
    
    [HYCNetService myWalletMyredPackListWithDict:dic onSuccess:^(NSDictionary *dictResult) {
        [weakSelf.view hideHUDLoading:0];
        NSArray *lists = dictResult[@"myredPackList"];
        if (_pageNo==1) {
            [_totalArr removeAllObjects];
        }
        
            _tishiLab.text = _isLoseEfficacy?@"已无更多可用红包":@"已无更多失效红包";
            
    
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
        
        weakSelf.tableView.mj_footer.hidden = lists.count<10;
        
        [weakSelf.totalArr addObjectsFromArray:[RedPackModel mj_objectArrayWithKeyValuesArray:lists]];
        
        [weakSelf.tableView reloadData];
        
    } andFailed:^(NSInteger code, NSString *errorMsg) {
        [weakSelf.view showHUDFailTips:errorMsg hideDelay:kHUDHideDelay];
        
        _pageNo--;
        [weakSelf.tableView.mj_header endRefreshing];
        [weakSelf.tableView.mj_footer endRefreshing];
    }];
    
}
@end
